<?php
function valida_email($address)
{
//
if (ereg('^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9_\.\-]+\.[a-zA-Z0-9_\.\-]+$',$address))
   return true;
else
   return false;
}

//****************************************************************************
function filled_out($form_vars)
{
//testa se cada vari�vel tem um valor
foreach ($form_vars as $key=>$value)
{
if (!isset($key) || ($value==''))
   return false;
}
return true;
}
//*****************************************************************************
	// VERIFICA CPF
	function validaCPF($cpf) {
	    $cpf= str_replace(".","",$cpf);
		$cpf= str_replace("-","",$cpf); 
		
		$soma = 0;
		if (strlen($cpf) <> 11)
			return false;
		// Verifica 1� digito		
		for ($i = 0; $i < 9; $i++) {			
			$soma += (($i+1) * $cpf[$i]);
		}
		$d1 = ($soma % 11);
		if ($d1 == 10) {
			$d1 = 0;
		}
		$soma = 0;
		// Verifica 2� digito
		for ($i = 9, $j = 0; $i > 0; $i--, $j++) {
			$soma += ($i * $cpf[$j]);
		}
		$d2 = ($soma % 11);
		if ($d2 == 10) {
			$d2 = 0;
		}		
		if ($d1 == $cpf[9] && $d2 == $cpf[10]) {
			return true;
		}
		else {
			return false;
		}
	}
//*****************************************************************************
	// VERFICA CNPJ
	function validaCNPJ($cnpj) {
	    $cnpj= str_replace(".","",$cnpj);
		$cnpj= str_replace("/","",$cnpj);
		$cnpj= str_replace("-","",$cnpj);  
	
		if (strlen($cnpj) <> 14)
			return false; 

		$soma = 0;
		
		$soma += ($cnpj[0] * 5);
		$soma += ($cnpj[1] * 4);
		$soma += ($cnpj[2] * 3);
		$soma += ($cnpj[3] * 2);
		$soma += ($cnpj[4] * 9); 
		$soma += ($cnpj[5] * 8);
		$soma += ($cnpj[6] * 7);
		$soma += ($cnpj[7] * 6);
		$soma += ($cnpj[8] * 5);
		$soma += ($cnpj[9] * 4);
		$soma += ($cnpj[10] * 3);
		$soma += ($cnpj[11] * 2); 

		$d1 = $soma % 11; 
		$d1 = $d1 < 2 ? 0 : 11 - $d1; 

		$soma = 0;
		$soma += ($cnpj[0] * 6); 
		$soma += ($cnpj[1] * 5);
		$soma += ($cnpj[2] * 4);
		$soma += ($cnpj[3] * 3);
		$soma += ($cnpj[4] * 2);
		$soma += ($cnpj[5] * 9);
		$soma += ($cnpj[6] * 8);
		$soma += ($cnpj[7] * 7);
		$soma += ($cnpj[8] * 6);
		$soma += ($cnpj[9] * 5);
		$soma += ($cnpj[10] * 4);
		$soma += ($cnpj[11] * 3);
		$soma += ($cnpj[12] * 2); 
		
		
		$d2 = $soma % 11; 
		$d2 = $d2 < 2 ? 0 : 11 - $d2; 
		
		if ($cnpj[12] == $d1 && $cnpj[13] == $d2) {
			return true;
		}
		else {
			return false;
		}
	} 
//************************************************************************************************
  function DataToStr($data) {
    return substr($data,8,2)."/".substr($data,5,2)."/".substr($data,0,4);
  }
//************************************************************************************************  
  function StrToData($data) {
    return substr($data,6,4)."-".substr($data,3,2)."-".substr($data,0,2);
  }
//************************************************************************************************
  function NumeroToMoeda($valor) {
    return number_format($valor, 2, ',', '.');
  }
//************************************************************************************************

function fastComboBox($sSource,$sConnString,$sValueField,$sDisplayField,$sSelected,$sBlankOption,$blBuffered)
{
	$sOutput = "";
	if (is_null($sSelected) || ($sSelected == ""))
	{
		$sSelected = " ";
	}

	$rsFast = mysql_query($sSource, $sConnString);
	if ($rsFast == false)
	{
		$sOutPut .= "<option value=''>" . "--- ---" . "</option>";
		return $sOutPut;
	}
	if (mysql_num_rows($rsFast) > 0)
	{
		if ($sBlankOption != "")
		{
			$sOutPut .= "<option value='-1'>" . $sBlankOption . "</option>";
		}
			
		while($aCOMBO = mysql_fetch_array($rsFast))
		{
			if ($sSelected != " ")
			{
				if ($aCOMBO[$sValueField] == $sSelected)
					$isSelected = " SELECTED ";
				else
					$isSelected = " ";
			}

			$sOutPut .= "<option " . $isSelected . "value=" . Chr(34) . $aCOMBO[$sValueField] . Chr(34) . ">" . $aCOMBO[$sDisplayField] . "</option>\n";
		}

	}else
		$sOutPut .= "<option value=''>" . "--- ---" . "</option>";
	
//	}else if ($sBlankOption != "")
//			{
//				$sOutPut .= "<option value=''>" . "--- ---" . "</option>";
//			}

	
	@mysql_free_result($rsFast);
					
	if ($blBuffered == "1")
		{
			return $sOutPut;
		}
	else
		{
			echo($sOutPut);
		}
}

function ComboBoxNumero($nNum_Inicio, $nNum_Selecionado, $nNum_Fim, $sBlankOption, $blBuffered)
{
	$sOutput = "";
	if (is_null($nNum_Selecionado) || ($nNum_Selecionado == "") || ($nNum_Selecionado == "0"))
	{
		$sSelected = " ";
	}

	if ($nNum_Inicio <= $nNum_Fim)
	{
		if ($sBlankOption != "")
		{
			$sOutPut .= "<option value='-1'>" . $sBlankOption . "</option>";
		}
			
		$i = $nNum_Inicio;
		while($i <= $nNum_Fim)
		{
			if ($sSelected != " ")
			{
				if ($i == $nNum_Selecionado)
					$isSelected = " SELECTED ";
				else
					$isSelected = " ";
			}

			$sOutPut .= "<option " . $isSelected . "value=" . $i . ">" . $i . "</option>\n";
			$i = $i + 1;
		}

	}else if ($sBlankOption != "")
			{
				$sOutPut .= "<option value=''>" . "--- ---" . "</option>";
			}
	
	if ($blBuffered == "1")
		{
			return $sOutPut;
		}
	else
		{
			echo($sOutPut);
		}
}


function ComboBoxMeses($Mes_Selecionado, $sBlankOption, $blBuffered)
{
	$sOutput = "";
	if (is_null($Mes_Selecionado) || ($Mes_Selecionado == "") || ($Mes_Selecionado == "0"))
	{
		$sSelected = " ";
	}

	if ($sBlankOption != "")
	{
		$sOutPut .= "<option value='-1'>" . $sBlankOption . "</option>";
	}
		
	$i = 1;
	while($i <= 12)
	{
		if ($sSelected != " ")
		{
			if ($i == $Mes_Selecionado)
				$isSelected = " SELECTED ";
			else
				$isSelected = " ";
		}
		
		switch($i) 
        { 
        	case 1: $mes = 'Janeiro'; break; 
        	case 2: $mes = 'Fevereiro'; break;
        	case 3: $mes = 'Março'; break;
        	case 4: $mes = 'Abril'; break;
        	case 5: $mes = 'Maio'; break;
        	case 6: $mes = 'Junho'; break;
        	case 7: $mes = 'Julho'; break;
        	case 8: $mes = 'Agosto'; break;
        	case 9: $mes = 'Setembro'; break;
        	case 10: $mes = 'Outubro'; break;
        	case 11: $mes = 'Novembro'; break;
        	case 12: $mes = 'Dezembro'; break;
        } 
		

		$sOutPut .= "<option " . $isSelected . "value=" . $i . ">" . $mes . "</option>\n";
	
		$i++;
	}

	if ($blBuffered == "1")
		{
			return $sOutPut;
		}
	else
		{
			echo($sOutPut);
		}
}

function ComboBoxSimNao($nNum_Selecionado, $sBlankOption, $blBuffered)
{
	$sOutput = "";
	if ($sBlankOption != "")
		$sOutPut .= "<option value='-1'>" . $sBlankOption . "</option>";
	else
		$sOutPut .= "<option value=''>" . "--- ---" . "</option>";
			
	if ($nNum_Selecionado == 'S') 
		$sOutPut .= "<option SELECTED value='S'>SIM</option>\n";
	else
		$sOutPut .= "<option value='S'>SIM</option>\n";		
	
	if ($nNum_Selecionado == 'N') 
		$sOutPut .= "<option SELECTED value='N'>N�O</option>\n";
	else
		$sOutPut .= "<option value='N'>N�O</option>\n";
		
	if ($blBuffered == "1")
		{
			return $sOutPut;
		}
	else
		{
			echo($sOutPut);
		}
}

function formata_valor_bd($valor)
{
	$valor_new = $valor;
	$limpa        = str_replace(".","",$valor_new);
	$limpa2       = str_replace(",",".",$limpa);	
	$valor_new  = $limpa2;
	return $valor_new;
}

function get_ultimo_dia_mes($mes, $ano)
{
	switch($mes) 
	{ 
		case 1: $dia = '31'; break; 
		case 2: {
					if (bcmod($ano, 4) != 0)
						$dia = '28';
					else
						$dia = '29';
					break;
				}
		case 3: $dia = '31'; break;
		case 4: $dia = '30'; break;
		case 5: $dia = '31'; break;
		case 6: $dia = '30'; break;
		case 7: $dia = '31'; break;
		case 8: $dia = '31'; break;
		case 9: $dia = '30'; break;
		case 10: $dia = '31'; break;
		case 11: $dia = '30'; break;
		case 12: $dia = '31'; break;
	}
	return $dia;
}
 function ver($nom){
 	
  print_r("<pre>");
  print_r($nom);
 	
 }

 function dataPorExtenso($data){
	
$dataConvertida = strtotime($data);	
  
$semana = date("w",$dataConvertida); 
$dia = date("j",$dataConvertida);
$mes = date("n",$dataConvertida);
$ano = date("Y",$dataConvertida);

$meses = array(1 => "Janeiro", "Fevereiro", "Março", "Abril", "Maio", "Junho","Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro");

$semanas = array("Domingo", "Segunda-Feira", "Terça-Feira", "Quarta-Feira", "Quinta-Feira", "Sexta-Feira", "Sabado");
 
 if ($data != ""){
	 return  "$dia de $meses[$mes] de $ano";
 }else{
   	
 	return "";
 }
 
}

?>
